default_platform(:ios)

platform :ios do
  # Defined in Appfile
  app_identifier = CredentialsManager::AppfileConfig.try_fetch_value(:app_identifier)
  team_id = CredentialsManager::AppfileConfig.try_fetch_value(:team_id)

  ioSwiftInfoPlistPath = "./Samples/iOS-Swift/iOS-Swift/Info.plist"

  lane :bump_bundle_version do
    set_info_plist_value(
      path: ioSwiftInfoPlistPath, 
      key: "CFBundleVersion", 
      value: ENV["FASTLANE_BUNDLE_VERSION"]
    )
  end

  # The version for all Info.plist must be a period-separated list of at most three non-negative integers
  # for App Store Connect. If we are on a preview we to remove these suffixes.
  lane :remove_preview_version_suffixes do
    version = get_version_number(xcodeproj: "./Samples/iOS-Swift/iOS-Swift.xcodeproj")
    version = version.split("-", -1)[0]

    set_info_plist_value(
      path: ioSwiftInfoPlistPath, 
      key: "CFBundleShortVersionString", 
      value: version
    )

    sentryInfoPlistPath = "./Sources/Sentry/Info.plist"
    set_info_plist_value(
      path: sentryInfoPlistPath, 
      key: "CFBundleShortVersionString", 
      value: version
    )
    set_info_plist_value(
      path: sentryInfoPlistPath, 
      key: "CFBundleVersion", 
      value: version
    )
  end

  desc "Upload iOS-Swift to TestFlight and symbols to Sentry"
  lane :ios_swift_to_testflight do

    keychain_name = "ios-swift.keychain"
    keychain_password = ENV["FASTLANE_KEYCHAIN_PASSWORD"]

    # CI can hang when Xcode is trying to sign the app because
    # a modal window pops up to access the signing key's keychain. This modal
    # lets the action hang. A workaround is to create a local keychain and
    # store the signing keys in there.
    # https://github.com/actions/virtual-environments/issues/1820#issuecomment-719549887
    # Doing this with Fastlane makes it easier to debug issues locally.
    sh("security delete-keychain #{keychain_name} || true") # Delete the keychain if it already exists
    sh("security create-keychain -p '#{keychain_password}'  #{keychain_name}")
    sh("security set-keychain-settings -lut 21600 #{keychain_name}")
    sh("security unlock-keychain -p '#{keychain_password}' #{keychain_name}")
    sh("security list-keychain -d user -s #{keychain_name}")

    sync_code_signing(
      type: "appstore", 
      keychain_name: keychain_name,
      keychain_password: keychain_password,
      readonly: true
    )

    app_store_connect_api_key(
      key_id: ENV["APP_STORE_CONNECT_KEY_ID"],
      issuer_id: ENV["APP_STORE_CONNECT_ISSUER_ID"],
      key_content: ENV["APP_STORE_CONNECT_KEY"]
    )

		build_app(
      workspace: "Sentry.xcworkspace",
      scheme: "iOS-Swift",
			include_bitcode: true,
      include_symbols: false,
			export_options: {
				method: "app-store",
				provisioningProfiles: { 
					app_identifier => "match AppStore io.sentry.sample.iOS-Swift"
				}
			},
      archive_path: "iOS-Swift"
    )
    
    testflight

    download_dsyms(
      username: ENV["FASTLANE_USER"],
      wait_for_dsym_processing: true,
      build_number: ENV["FASTLANE_BUNDLE_VERSION"]
    )

    sentry_upload_dsym(
      auth_token: ENV["SENTRY_AUTH_TOKEN"],
      org_slug: 'sentry-sdks',
      project_slug: 'sentry-cocoa',
      symbol_maps: 'iOS-Swift.xcarchive/BCSymbolMaps/'
    )
  end

  lane :ui_tests do
    run_tests(workspace: "Sentry.xcworkspace",
      devices: ["iPhone 8"],
      scheme: "iOS-SwiftUI")

    run_tests(workspace: "Sentry.xcworkspace",
        devices: ["iPhone 8"],
        scheme: "iOS-Swift")

    run_tests(workspace: "Sentry.xcworkspace",
      devices: ["iPhone 8"],
      scheme: "iOS-ObjectiveC")

    run_tests(workspace: "Sentry.xcworkspace",
      devices: ["Apple TV"],
      scheme: "tvOS-Swift")

  end

end
